# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Sebastian Wiesner <lunaryorn googlemail com>
# Contributor: Igor Ramos Tiburcio <irtigor@yahoo.com.br>
# Contributor: Wieland Hoffmann <themineo+aur@gmail.com>
# Contributor: Hervé Cauwelier <herve ¤ oursours.net>
# Contributor: Felix Kaiser <felix.kaiser@fxkr.net>

pkgname=python-py
pkgver=1.11.0
_commit=447bac514febbb5433963582103d48bb27b3db17  # refs/tags/1.11.0
pkgrel=4
pkgdesc="library with cross-python path, ini-parsing, io, code, log facilities"
arch=('any')
license=('MIT')
url="https://py.readthedocs.io/"
depends=(
  'python'
  'python-apipkg'
  'python-iniconfig'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-setuptools-scm'
  'python-wheel'
)
checkdepends=(
  'python-attrs'
  'python-pytest'
  'subversion'
)
optdepends=(
  'python-pytest: for py.test module'
)
source=(
  "git+https://github.com/pytest-dev/py.git#commit=$_commit"
  $pkgname-1.11.0-devendor.patch
)
sha512sums=('SKIP'
            'edab08dc6331637b20e385e14622e0df147c2a0b6974f31683ccd9ca1ba3513c167e6b7e0ddbc664ef1c208ea3eeee87c6ff0e2b7810bc15a71c61d382717eca')

prepare() {
  cd py
  # fix for pytest >= 4: https://github.com/pytest-dev/py/issues/209
  git show f4ed62ad39f289d09b3efdfed7305f935ce60bfc -- testing | git apply -

  # devendor python-apipkg and python-iniconfig
  git apply -3 ../$pkgname-1.11.0-devendor.patch
  rm -r py/_vendored_packages
}

build() {
  cd py
  python -m build --wheel --no-isolation
}

check() {
  local pytest_options=(
    # upstream testing is just broken: https://github.com/pytest-dev/py/issues/288
    --deselect testing/io_/test_terminalwriter.py::test_getdimensions
    --deselect testing/code/test_excinfo.py::test_excinfo_repr
    --deselect testing/code/test_excinfo.py::test_excinfo_str
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-locals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-locals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-locals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-nolocals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-nolocals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-tbfilter-nolocals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-locals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-locals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-locals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-nolocals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-nolocals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[funcargs-nofilter-nolocals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-locals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-locals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-locals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-nolocals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-nolocals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-tbfilter-nolocals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-locals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-locals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-locals-no]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-nolocals-long]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-nolocals-short]'
    --deselect 'testing/code/test_excinfo.py::TestFormattedExcinfo::test_format_excinfo[nofuncargs-nofilter-nolocals-no]'
    --deselect 'testing/io_/test_capture.py::test_capturing_and_logging_fundamentals[StdCapture]'
    --deselect 'testing/io_/test_capture.py::test_capturing_and_logging_fundamentals[StdCaptureFD]'
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd py
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="test_dir/$site_packages:$PYTHONPATH"
  pytest -vv "${pytest_options[@]}"
}

package() {
  cd py
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}
